<template>
  <div>
    <el-dialog title="选择量表" :visible.sync="isAddScale" width="60%" :before-close="handleClose" :close-on-click-modal='false'>
      <el-table ref="multipleTable" :data="tableData" tooltip-effect="dark" style="width: 100%" max-height="250" size="mini" border @selection-change="handleSelectionChange">
        <el-table-column fixed type="selection" width="55" :selectable="selectable"></el-table-column>
        <el-table-column prop="student_title" label="名称" align="center">
          <template slot-scope="scope">
            <span v-if="scope.$index === 0">
              <el-input v-model="title" placeholder="请输入名称" size="mini"></el-input>
            </span>
            <span v-else> {{scope.row.student_title}} </span>
          </template>
        </el-table-column>
        <el-table-column prop="topics_num" label="题量" align="center"> </el-table-column>
        <el-table-column prop="count" label="时长" width="120" align="center"> </el-table-column>
        <el-table-column prop="operation" label="操作" align="center">
          <template slot-scope="scope">
            <template v-if="scope.$index ===0">
              <el-button type="primary" round size="mini" @click="examine('search',scope.row)">查询</el-button>
              <el-button type="text" @click="examine('remove',scope.row)">清除</el-button>
            </template>
          </template>
        </el-table-column>
      </el-table>
      <div class="distribution"><span class="distributionTitle">已选中</span><span class="distributionPrompt">共{{this.select.totalNumber}}个量表，已选中{{this.select.selected}}个，已选{{this.select.num}}题，预计{{this.select.time}}分钟完成</span></div>
      <simple-table :tableConfig="tableConfig" :tableData="selectTableData" @haveChosen="haveChosen"></simple-table>
      <span slot="footer" class="dialog-footer">
        <el-button @click="close()">取 消</el-button>
        <el-button type="primary" @click="check()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import simpleTable from '@/components/simpleTable.vue'
import {APISINGLELIST} from '@/common/api.js'
export default {
  props: ['isAddScale', 'list'],
  components: {
    simpleTable
  },
  data () {
    return {
      tableConfig: {
        load: false,
        size: 'small', // 表格大小
        style: 'width:100%', // 表格宽度
        align: 'center',
        border: true,
        config: [
          {name: '序号', type: 'index', width: '60px'},
          {id: 'student_title', name: '名称'},
          {id: 'topics_num', name: '题量'},
          {id: 'count', name: '时长'}
        ]
      },
      selectTableData: [],
      tableData: [],
      title: '',
      select: {}
    }
  },
  created () {
    this.tableList()
    this.selectTableData = this.list
  },
  methods: {
    tableList () {
      APISINGLELIST({
        title: this.title
      }).then((res) => {
        if (res.status === 1) {
          this.tableData = res.data.list
          this.tableData.splice(0, 0, this.seacherTab)
          this.toggleSelection(this.list)
          this.select.totalNumber = res.data.totalNumber
        } else {
          this.$message.error(res.msg)
          this.tableData = []
          this.tableData.push(this.seacherTab)
        }
      }).catch((err) => {
        this.$message.error(err.msg)
      })
    },
    toggleSelection (rows) {
      if (rows) {
        this.$nextTick(() => {
          rows.forEach(row => {
            for (var i = 1; i < this.tableData.length; i++) {
              if (row.id === this.tableData[i].id) {
                this.$refs.multipleTable.toggleRowSelection(this.tableData[i])
              }
            }
          })
        })
      } else {
        this.$refs.multipleTable.clearSelection()
      }
    },
    handleClose () {
      this.$emit('noAddScale', this.list)
    },
    close () {
      this.$emit('noAddScale', this.list)
    },
    selectable (row, index) {
      if (index !== 0) {
        return true
      } else {
        return false
      }
    },
    examine (type, e) { // 操作按钮
      if (type === 'search') {
        this.tableList()
      } else if (type === 'remove') {
        this.title = ''
        this.tableList()
      }
    },
    handleSelectionChange (val) {
      this.selectTableData = val
      this.select.selected = this.selectTableData.length
      this.select.num = 0
      this.select.time = 0
      this.selectTableData.forEach(item => {
        this.select.num += Number(item.topics_num)
        this.select.time += Number(item.count)
      })
    },
    check () {
      this.$emit('noAddScale', this.selectTableData)
    },
    haveChosen (val, totalNumber) {
      this.select.totalNumber = totalNumber
      this.selectTableData = val
      this.select.selected = this.selectTableData.length
      this.select.num = 0
      this.select.time = 0
      this.selectTableData.forEach(item => {
        this.select.num += Number(item.topics_num)
        this.select.time += Number(item.count)
      })
    }
  }
}
</script>
<style lang="scss">
@import 'src/assets/style/varibles.scss';
.distribution{
  height: 40px;
  line-height: 40px;
  margin-top: 20px;
}
.distributionTitle{
  color: $mainFontColor;
  font-size: $secondarySize;
  display: inline-block;
  padding-right: 10px;
}
</style>
